package com.sushi.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sushi.exception.BusinessException;
import com.sushi.repository.entity.ActivityEntity;
import com.sushi.repository.mapper.ActivityMapper;
import com.sushi.service.ActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sushi.response.ResponseBO;
import com.sushi.response.ResponsePageBO;
import com.sushi.response.ActivityResp;
import com.sushi.request.ActivityReq;
import com.sushi.request.ActivityPageReq;
import com.sushi.utils.*;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 活动 服务实现类
 * @date 2023-05-02
 * @time 17:12:13
 * @author Administrator
 */
@Slf4j
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, ActivityEntity> implements ActivityService {

	@Autowired
	private ActivityMapper activityMapper;

    

    /**
     * 新增
     */
    @Override
    public ResponseBO<ActivityResp> add(ActivityReq req) {
        ActivityEntity entity = BeanUtil.convert(req, ActivityEntity.class);
        if (ObjectUtil.isEmpty(entity)) {
            throw new BusinessException("保存失败");
        }
        entity.insert();
        return ResponseUtil.ok(BeanUtil.convert(entity, ActivityResp.class));
    }

    /**
     * 逻辑删除
     */
    @Override
    public ResponseBO<ActivityResp> logicRemove(ActivityReq req) {
        if (StringUtil.isBlank(req.getId())) {
            throw new BusinessException("id不能为空");
        }
        ActivityEntity entity = new ActivityEntity();
        entity.setId(req.getId());
        entity.setDeleted(1L);
        entity.updateById();
        return ResponseUtil.ok(BeanUtil.convert(entity, ActivityResp.class));
    }

    /**
     * 更新
     */
    @Override
    public ResponseBO<ActivityResp> updateByField(ActivityReq req) {
        if (ObjectUtil.isEmpty(req) || StringUtil.isBlank(req.getId())) {
            throw new BusinessException("必要参数不能为空");
        }
        ActivityEntity entity = getById(req.getId());
        if (ObjectUtil.isEmpty(entity)) {
            return ResponseUtil.error("信息不存在，无法更新");
        }
        if (StringUtil.isNotBlank(req.getTitle())) {
            entity.setTitle(req.getTitle());
        }
        if (StringUtil.isNotBlank(req.getContent())) {
            entity.setContent(req.getContent());
        }
        if (StringUtil.isNotBlank(req.getFileUrls())) {
            entity.setFileUrls(req.getFileUrls());
        }
        if (StringUtil.isNotBlank(req.getPictureUrls())) {
            entity.setPictureUrls(req.getPictureUrls());
        }
        if (ObjectUtil.isNotEmpty(req.getLimitPersonNum())) {
            entity.setLimitPersonNum(req.getLimitPersonNum());
        }
        if (StringUtil.isNotBlank(req.getPlace())) {
            entity.setPlace(req.getPlace());
        }
        if (StringUtil.isNotBlank(req.getCondition())) {
            entity.setCondition(req.getCondition());
        }
        if (StringUtil.isNotBlank(req.getStatus())) {
            entity.setStatus(req.getStatus());
        }
        if (StringUtil.isNotBlank(req.getBackReason())) {
            entity.setBackReason(req.getBackReason());
        }

        entity.updateById();
        log.info("更新成功：{}", JSON.toJSONString(entity));
        return ResponseUtil.ok(BeanUtil.convert(entity, ActivityResp.class));

    }

    /**
     * 列表
     */
    @Override
    public ResponsePageBO<ActivityResp> listByCondition(ActivityPageReq req) {
        if (ObjectUtil.isEmpty(req) || ObjectUtil.isEmpty(req.getPageNum()) || ObjectUtil.isEmpty(req.getPageSize())) {
            throw new BusinessException("必要参数不能为空");
        }
        LambdaQueryWrapper<ActivityEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ActivityEntity::getDeleted, 0L);
        wrapper.eq(StringUtil.isNotBlank(req.getId()), ActivityEntity::getId, req.getId());
        wrapper.eq(StringUtil.isNotBlank(req.getTitle()), ActivityEntity::getTitle, req.getTitle());
        wrapper.eq(StringUtil.isNotBlank(req.getContent()), ActivityEntity::getContent, req.getContent());
        wrapper.eq(StringUtil.isNotBlank(req.getFileUrls()), ActivityEntity::getFileUrls, req.getFileUrls());
        wrapper.eq(StringUtil.isNotBlank(req.getPictureUrls()), ActivityEntity::getPictureUrls, req.getPictureUrls());
        wrapper.eq(ObjectUtil.isNotEmpty(req.getLimitPersonNum()), ActivityEntity::getLimitPersonNum, req.getLimitPersonNum());
        wrapper.eq(StringUtil.isNotBlank(req.getPlace()), ActivityEntity::getPlace, req.getPlace());
        wrapper.eq(StringUtil.isNotBlank(req.getCondition()), ActivityEntity::getCondition, req.getCondition());
        wrapper.eq(StringUtil.isNotBlank(req.getStatus()), ActivityEntity::getStatus, req.getStatus());
        wrapper.eq(StringUtil.isNotBlank(req.getBackReason()), ActivityEntity::getBackReason, req.getBackReason());

        wrapper.orderByDesc(ActivityEntity::getGmtCreateTime);

        Page<ActivityEntity> page = page(new Page<>(req.getPageNum(), req.getPageSize()), wrapper);
        if (ObjectUtil.isEmpty(page) || CollectionUtil.isEmpty(page.getRecords())) {
            return ResponsePageUtil.ok(req, new ArrayList<>(), 0L);
        }
        List<ActivityResp> convert = BeanUtil.convert(page.getRecords(), ActivityResp.class);
        return ResponsePageUtil.ok(req, convert, page.getTotal());

    }

    /**
     * 详情
     */
    @Override
    public ResponseBO<ActivityResp> detail(ActivityReq req) {
        if (ObjectUtil.isEmpty(req) || StringUtil.isBlank(req.getId())) {
            throw new BusinessException("必要参数不能为空");
        }
        ActivityEntity entity = getById(req.getId());
        if (ObjectUtil.isEmpty(entity)) {
            throw new BusinessException("对象不存在");
        }
        return ResponseUtil.ok(BeanUtil.convert(entity, ActivityResp.class));
    };

}